/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;
import BE.Hallazgo;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;

import org.hibernate.Session;
import org.hibernate.Transaction;
/**
 *
 * @author CarLita
 */
public class HallazgoDao {
    Session session;
       
    public boolean eliminar(Hallazgo hallazgo) {
        boolean ok =true;        
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            Transaction tx = session.beginTransaction();
            session.delete(hallazgo);
            tx.commit();
            session.close();
        }
        catch(Exception e){
            ok = false;
            System.out.println(e.getMessage());
        }
        return ok;        
    }
    

    public Hallazgo buscarPorId(int idHallazgo) {
         session = HibernateUtil.getSessionFactory().openSession();
        String hql = "from Hallazgo hallazgo where hallazgo.idHallazgo= ? ";
        Query query = session.createQuery(hql);
        query.setInteger(0, idHallazgo);
        List resultList = null;

        try {
            session.beginTransaction();
            resultList = query.list();
            session.getTransaction().commit();
            session.close();
            return (Hallazgo)resultList.get(0);
        } catch (HibernateException he) {
            he.printStackTrace();
        }

        session.close();
        return null;
    }
    
    public List buscarPorCriterios(Hallazgo hallazgo) {        
        session = HibernateUtil.getSessionFactory().openSession();
        String hql = "from Hallazgo hallazgo where hallazgo.titulo like concat('%',?,'%')"
                + " and hallazgo.proyecto.idproyecto = ?";
        if (hallazgo.getScore()>0){
            hql+= " and hallazgo.score = ?";
        }                   
        if (hallazgo.getPapelTrabajo()!= null && hallazgo.getPapelTrabajo().getIdPapelTrabajo()!=-1){
            hql+= "and hallazgo.documento.idPapelTrabajo = ?";
        }
        
        Query query = session.createQuery(hql);
        query.setString(0, hallazgo.getTitulo());
        query.setInteger(1, hallazgo.getProyecto().getIdproyecto());
        if (hallazgo.getScore()>0){
            query.setInteger(2, hallazgo.getScore());
        }           
        if (hallazgo.getPapelTrabajo()!= null && hallazgo.getPapelTrabajo().getIdPapelTrabajo()!=-1){
            query.setInteger(3, hallazgo.getPapelTrabajo().getIdPapelTrabajo());
        }
        
        List resultList = null;

        try {
            session.beginTransaction();
            resultList = query.list();
            session.getTransaction().commit();
            session.close();
            return resultList;
        } catch (HibernateException he) {
            he.printStackTrace();
        }

        session.close();
        return null;
    }
    
    public boolean guardar(Hallazgo hallazgo) {
        boolean ok = true;
        session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try {
            tx.begin();
            session.merge(hallazgo);
            tx.commit();
        } catch (HibernateException he){
            tx.rollback();
            he.printStackTrace();
            ok=false;
        } finally {
            session.close();
        }
        return ok;
    }
    
    public List buscarPorIdProyecto(int idProyecto){
        session = HibernateUtil.getSessionFactory().openSession();
        String hql = "from Hallazgo hallazgo where hallazgo.proyecto.idproyecto = ?";
                
        Query query = session.createQuery(hql);        
        query.setInteger(0, idProyecto);
                
        List resultList = null;

        try {
            session.beginTransaction();
            resultList = query.list();
            session.getTransaction().commit();
            session.close();
            return resultList;
        } catch (HibernateException he) {
            he.printStackTrace();
        }

        session.close();
        return null;
    }
}
